﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KerriganDataContract.Intructor;
using KerriganDataContract;
using System.ServiceModel;
using KerriganDataContract.Session;

namespace KerriganService.DAC
{
    public static class InstructorDAC
    {
        #region Static Method

        public static InstructorDC CheckLoginInstructor(string login, string password)
        {
            InstructorDC dc = null;
            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    TInstructor entity;
                    entity = db.TInstructors.Where(t => t.TPerson.NickName == login && t.TPerson.Password == password).FirstOrDefault();
                    if (entity != null)
                    {
                        dc = CopyEntityToBo(entity);
                    }
                    
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return dc;
        }


        public static InstructorDC GetInstructor(int id)
        {
            InstructorDC dc = new InstructorDC();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    TInstructor entity = new TInstructor();
                    entity = db.TInstructors.Where(t => t.IdInstructor == id).FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return dc;
        }

        public static List<InstructorDC> GetListInstructor()
        {
            List<InstructorDC> list = new List<InstructorDC>();
            
            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    List<TInstructor> entities = new List<TInstructor>();
                    entities = db.TInstructors.ToList();
                    list = CopyListEntitiesToListBos(entities);
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        /// <summary>
        /// Add and Update method for a Instructor Object
        /// </summary>
        /// <param name="dc"></param>
        public static void SetInstructor(InstructorDC bo)
        {
            TInstructor entity = new TInstructor();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    if (bo.IdInstrutorDC == 0)
                    {
                        entity = InstructorDAC.CopyBoToEntity(bo);
                        db.AddToTInstructors(entity);
                        db.SaveChanges();
                    }
                    else
                    {
                        entity = db.TInstructors.Where(t => t.IdInstructor == bo.IdInstrutorDC).FirstOrDefault();
                        entity = InstructorDAC.CopyBoToEntity(bo);
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public static void DeleteInstructor(int id)
        {
            TInstructor entity = new TInstructor();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    entity = db.TInstructors.Where(t => t.IdInstructor == id).FirstOrDefault();
                    db.DeleteObject(entity);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public static List<vwGetInstructor> GetListInstructorsToDisplay()
        {
            List<vwGetInstructor> list = new List<vwGetInstructor>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    list = db.vwGetInstructors.ToList();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        public static List<vwGetInstructorAvailability> GetListInstructorsAvailabilityToDisplay()
        {
            List<vwGetInstructorAvailability> list = new List<vwGetInstructorAvailability>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    list = db.vwGetInstructorAvailabilities.ToList();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        public static List<InstructorAvailabilityDC> GetListInstructorAvailable()
        {
            List<InstructorAvailabilityDC> list = new List<InstructorAvailabilityDC>();

            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    list = CopyListEntitiesToListBos(db.TInstructorAvailabilities.ToList());
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return list;
        }

        public static void AddAvaibilityForInstructor(InstructorAvailabilityDC dc)
        {
            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    TInstructorAvailability entity = new TInstructorAvailability();
                    entity = CopyBoToEntity(dc);
                    db.AddToTInstructorAvailabilities(entity);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public static void DeleteInstructorAvaibility(int id)
        {
            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    TInstructorAvailability entity = new TInstructorAvailability();
                    entity = db.TInstructorAvailabilities.Where(t => t.IdInstructorAvailability == id).FirstOrDefault();
                    db.DeleteObject(entity);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public static List<GetInstructorAvaibleBySession_Result> GetListInstructorBySession(SessionDC bo)
        {
            List<GetInstructorAvaibleBySession_Result> res = new List<GetInstructorAvaibleBySession_Result>();
            
            try
            {
                using (KerriganDBEntities db = new KerriganDBEntities())
                {
                    res = db.GetInstructorAvaibleBySession(bo.Date, bo.EndDate).ToList();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return res;
        }

        #endregion

        #region Entity/Bo method

        public static InstructorDC CopyEntityToBo(TInstructor entity)
        {
            InstructorDC bo = new InstructorDC();

            try
            {
                bo.IdInstrutorDC= entity.IdInstructor;
                bo.Person = PersonDAC.CopyEntityToBo(entity.TPerson);
                bo.FullName = bo.Person.FirstName + " " + bo.Person.LastName;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return bo;
        }

        public static InstructorAvailabilityDC CopyEntityToBo(TInstructorAvailability entity)
        {
            InstructorAvailabilityDC bo = new InstructorAvailabilityDC();

            try
            {
                bo.EndDate = entity.EndDate;
                bo.Id = entity.IdInstructorAvailability;
                bo.Instructor = InstructorDAC.CopyEntityToBo(entity.TInstructor);
                bo.StartDate = entity.StartDate;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return bo;
        }

        

        public static TInstructor CopyBoToEntity(InstructorDC bo)
        {
            TInstructor entity = new TInstructor();

            try
            {
                entity.IdInstructor = bo.IdInstrutorDC;
                entity.TPerson = new TPerson();
                entity.TPerson = PersonDAC.CopyBoToEntity(bo.Person);
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return entity;
        }

        public static TInstructorAvailability CopyBoToEntity(InstructorAvailabilityDC bo)
        {
            TInstructorAvailability entity = new TInstructorAvailability();

            try
            {
                entity.EndDate = bo.EndDate;
                entity.IdInstructorAvailability = bo.Id;
                entity.IdInstructorFK = bo.Instructor.IdInstrutorDC;
                entity.StartDate = bo.StartDate;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }

            return entity;
        }

        public static List<InstructorDC> CopyListEntitiesToListBos(List<TInstructor> entities)
        {
            List<InstructorDC> bo = new List<InstructorDC>();

            try
            {
                foreach (TInstructor entity in entities)
                {
                    bo.Add(CopyEntityToBo(entity));
                }
                return bo;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public static List<InstructorAvailabilityDC> CopyListEntitiesToListBos(List<TInstructorAvailability> entities)
        {
            List<InstructorAvailabilityDC> bo = new List<InstructorAvailabilityDC>();

            try
            {
                foreach (TInstructorAvailability entity in entities)
                {
                    bo.Add(CopyEntityToBo(entity));
                }
                return bo;
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion
    }
}